//
//  PortableContacts.m
//  MySpaceID
//
//  Created by Haider Sabri on Thursday6/11/09.
//  Copyright 2009 MySpace, Inc. All rights reserved.
//

#import "PortableContacts.h"
#import "V2Endpoints.h"
#import "OAServiceTicket.h"

@implementation PortableContacts
@synthesize context;
@synthesize responseData;

NSData *responseData = nil;

+ (PortableContacts*) apiWithContext:(SecurityContext*) context
{
	PortableContacts *api = [[[PortableContacts alloc] init] autorelease];
	api.context = [context retain];
	return api;
}

- (void) dealloc{

	[context release];
	self.responseData = nil;
	[super dealloc];
}


#pragma mark -
#pragma mark API Methods

- (NSData*) getSelf
{
	responseData = nil;
	if(context)
	{
		NSURL *url = (NSURL*)[NSURL URLWithString: V2_SELF];
		[context makeRequest:url :@"GET":nil:self];
		
	}
	return responseData;
}
- (NSData*) getSelf:(NSArray*) keys
{
	responseData = nil;
	if(context)
	{
		NSString *keyList = @"";
		for (NSString *key in keys) {
			keyList = [keyList stringByAppendingString:[NSString stringWithFormat:@"%@;", key]];
		}
		keyList = [keyList substringToIndex:[keyList length] - 1];
		
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V2_SELF_WITH_FIELDS, keyList]];
		[context makeRequest:url :@"GET":nil:self];
		
	}
	return responseData;
}
- (NSData*) getFriends
{
	responseData = nil;
	if(context)
	{
		NSURL *url = (NSURL*)[NSURL URLWithString: V2_FRIENDS];
		[context makeRequest:url :@"GET":nil:self];
		
	}
	return responseData;
}

#pragma mark -
#pragma mark OAResponseDelegate methods

- (void)apiTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
	NSString *dataOutput = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	
	NSLog(dataOutput);
	NSLog( [[[ticket response] URL] absoluteString]);
	NSLog( [NSString stringWithFormat:@"%d", [(NSHTTPURLResponse*)[ticket response ] statusCode]]);
	if (ticket.succeeded) {
		self.responseData = data;
	}
	[dataOutput release];
}




-(void) apiTicket:(OAServiceTicket *)ticket didFinishWithError:(NSError *)error {
	
	NSLog(@"error came back");
}


@end
